import math


def traversal(array, l):
    """递归方式, 遍历多维数组"""
    for a in array:
        if type(a) is not list:
            print(a, end=' ')
            # # l.append(a)
            # l += [a]
        else:
            traversal(a, l)


def is_prime(n):
    """素数判定"""
    if type(n) is not int:
        print('is not int', type(n))
        return False
    if n <= 2:
        return False
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True


def div_prime(n):
    """素数质因数分解"""
    # todo 如何存入一个数组呢(不通过传参方式)
    if n < 2:
        print(n)
    for i in range(2, n):
        if n % i == 0 and not is_prime(n):
            print(i)
            div_prime(n // i)
            return
    print(n)


def hanoi(n, a, b, c):  # 此时的abc是argument 形参
    """汉诺塔
    分两个步骤:
    1. 将n-1的积木从A放到B柱, 然后将第n个积木从A放到C柱
    2. 将n-1积木从B移到C
    """
    if n == 1:
        # 移动最底的环
        print(f'{a} -- {c} -- {n}')
    else:
        hanoi(n - 1, a, c, b)  # 此时的abc是parameter 实参
        # 移动最顶的环
        print(f'{a} ~~ {c} ~~ {n}')
        hanoi(n - 1, b, a, c)


def hanoi_02(n, a, b, c):
    """汉诺塔
    分两个步骤:
    1. 将n-1的积木从A放到B柱, 然后将第n个积木从A放到C柱
    2. 将n-1积木从B移到C
    """
    if n != 0:
        hanoi_02(n - 1, a, c, b)
        print(f'{a} -- {c} -- {n}')
        hanoi_02(n - 1, b, a, c)


def traversal_02(arr, l):
    # print('traversal_02: ', end=' ')
    for i in arr:
        if type(i) is not list:
            print(i, end=' ')
        else:
            traversal_02(i, l)
    # print('\n')


if __name__ == '__main__':
    arr = [[0], 1, 2, 3, [4, 5, [6, 7, [8, 9]]], [10, 11]]
    l = []
    print('traversal: ', end=' ')
    traversal(arr, l)
    print('\n', l)
    print('traversal 02: ', end=' ')
    traversal_02(arr, l)
    print('\n', is_prime(61))
    print('----------')
    div_prime(20)
    hanoi(3, 'A', 'B', 'C')
    print('-----')
    # hanoi_02(3, 'A', 'B', 'C')
